/*
 * Copyright (C) 2010 Yury Kudryashov.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package name.kudryashov.functions;

import name.kudryashov.base.Argument;

public class Predicates {

    private Predicates() {}

    public static <T> Predicate<T> alwaysTrue() {
        return new Predicate<T>() {
            public boolean apply(T arg) {
                return true;
            }

            public String toString() {
                return "true";
            }
        };
    }

    public static <T> Predicate<T> alwaysFalse() {
        return new Predicate<T>() {
            public boolean apply(T arg) {
                return false;
            }

            public String toString() {
                return "false";
            }
        };
    }

    public static <T> Predicate<T> isNull() {
        return new Predicate<T>() {
            public boolean apply(T arg) {
                return arg == null;
            }

            public String toString() {
                return "isNull";
            }
        };
    }

    public static <T> Predicate<T> isNotNull() {
        return Compose.not(Predicates.<T>isNull());
    }

    public static <T> Predicate2<T, T> same() {
        return new Predicate2<T, T>() {
            public boolean apply(T arg1, T arg2) {
                return arg1 == null ? arg2 == null : arg1 == arg2;
            }

            public String toString() {
                return "==";
            }
        };
    }

    public static <T> Predicate2<T, T> equals() {
        return new Predicate2<T, T>() {
            public boolean apply(T arg1, T arg2) {
                return arg1 == null ? arg2 == null : arg1.equals(arg2);
            }

            public String toString() {
                return "equals";
            }
        };
    }

    public static <T> Predicate<T> sameAs(T t) {
        return Compose.of(Predicates.<T>same()).bind1(t);
    }

    public static <T> Predicate<T> equalsTo(T t) {
        return Compose.of(Predicates.<T>equals()).bind1(t);
    }

    public static <T> Function<T, Boolean> toFunction(final Predicate<T> p) {
        Argument.requireNotNull(p, "p");
        return new Function<T, Boolean>() {
            public Boolean apply(T arg) {
                return p.apply(arg);
            }

            public String toString() {
                return p.toString();
            }
        };
    }

    public static <T> Predicate<T> instanceOf(final Class<?> c) {
        Argument.requireNotNull(c, "c");
        return new Predicate<T>() {
            public boolean apply(T arg) {
                return c.isInstance(arg);
            }

            public String toString() {
                return "isInstanceOf" + c.getSimpleName();
            }
        };
    }

    /**
     * Returns a complement ("not") predicate for the given one - the returned predicate returns true when
     * the given one returns false and vice versa.
     * @param p predicate, not null
     * @param <T> type of the predicate
     * @return the new predicate
     * @throws NullPointerException predicate is null
     */
    public static <T> Predicate<T> complement(final Predicate<T> p) {
        return Compose.not(p);        
    }

}
